<?php
// +----------------------------------------------------------------------
// | 商标展示(商标列表、详情页、下单操作)
// +----------------------------------------------------------------------
// | Copyright (c) 2015
// +----------------------------------------------------------------------
// | Author: baiping 125618036@qq.com http://www.webipcode.com
// +----------------------------------------------------------------------
// |2015-12-9 下午3:20:50
// +----------------------------------------------------------------------

class Trademark extends MY_Controller{
	
	public function detail(){
		$cat_id = $this->uri->segment(3, 0);
		$id = $this->uri->segment(4, 0);
		$status = 0;
		if ( $cat_id > 0 ){
			//通过类别id获取类别名称 
			$this->load->model('Catgory_model', 'catgory');
			$catgory_info = $this->catgory->getData(0, array('pid'=>157) );
			$cargory_info_new = array();
			foreach ( $catgory_info AS $key => $val ){
				$cargory_info_new[$val->id] = $val;
			}
// 			var_dump( $cargory_info_new );
			//如果传用商标产品id时，获取商标产品详情（如果没有传递商品id,则获取当前分类下的最新的一条数据）
			$this->load->model('Trademark_attr_model', 'trademark');
			if ( $id > 0 ){
				$trademark_info = $this->trademark->getFind( array('id'=>$id) );
			}else{
				$trademark_info = $this->trademark->getFind( array('cat_id'=>$cat_id) );
			}	
			//获取所有商标产品
			$field = 'id,cat_id,title,service_price,officer_price,thumb';
			$trademark_list = $this->trademark->getData(array('cat_id'=>$cat_id) , $field);
			
			if ( !$trademark_list ){
				$status = 1;
			}else{
// 				var_dump( $trademark_info );
// 				var_dump( $trademark_list );
			}
			
		}else{
			$status = 1;
		}
		$this->load->vars('status', $status);
		$this->load->vars('cat_id', $cat_id);
		$this->load->vars('catgory_list', $cargory_info_new);//获取类别信息
		$this->load->vars('trademark_list', $trademark_list);//商标数据集
		$this->load->vars('trademark_info', $trademark_info);//读取当前商品详情
		$this->load->vars('meta_title', $trademark_info->title);
		$this->load->view('trademark/detail');	
		
	}
	
	/**
	 * 商标申请下单
	 */
	public function apply(){
		$id = intval( $this->input->get('id') );
		$this->load->model('Trademark_attr_model', 'trademark');
		$info = $this->trademark->getFind( array('id'=>$id), "id,service_price,officer_price,state" );
		$success = !empty( $info ) ? 1 : 0;
		
		$this->load->vars('success', $success);
		$this->load->vars('info', $info);
		$this->load->view('trademark/apply');
	}
	
	/**
	 * 商标下单操作
	 */
	public function applyed(){
		if ( IS_POST ){
			$verify =  $this->input->post('verify');
			if ( false == check_verify( $verify ) ){
				ajaxreturn(0, '验证码错误,请重新填写');
				return false;
			}
			$id = intval( $this->uri->segment(3,0) );
			if ( !is_numeric( $id )  && $id == 0 ){
				ajaxreturn(0, '参数错误');
			}
			//获取商标产品详情
			$info = $this->getTrademarkInfo($id);
			$this->load->model('Trademark_order_model', 'order');
			$insert_id = $this->order->addData( $info );
			if ( $insert_id ){
				ajaxreturn(1, '下单成功，业务员会尽快与您联系');
			}else{
				ajaxreturn(0, '操作失败');
			}
			
		}else{
			ajaxreturn(0, '非法请求');
		}
	}
	
	/**
	 * 获取商标信息
	 * @param unknown_type $id
	 */
	private function getTrademarkInfo($id){
		$this->load->model('Trademark_attr_model', 'trademark');
		$info = $this->trademark->getFind( array('id'=>$id), "id,service_price,officer_price,state" );
		if ( empty( $info ) ){
			ajaxreturn(0, '参数错误');
		}elseif ( $info->state == 1 ){
			ajaxreturn(0, '产品已下架');
		}
		return $info;
	}
	
	
	
}